const fetch = require('../../utils/fetch.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    activeIndex: 0,
    toView: 'a0',
    currentType: 0,
    currentIndex: 0,
    cartList: [],
    sumMoney: 0,
    cupNumber: 0,
    showCart: false,
    scrollTop: 100,
    order_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '努力加载中...'
    })
    fetch('food/list').then(res => { //请求数据
      console.log(res)
      wx.hideLoading()
      this.setData({
        listData: res.data,
        loading: true
      })
    })
  },
  selectMenu: function(e) { //菜单选择
    let index = e.currentTarget.dataset.index
    console.log(e)
    this.setData({
      activeIndex: index,
      toView: "a" + index
    })
    console.log(this.data.toView)
  },
  addToCart: function(e) { //添加商品
    console.log("增加", e)
    var type = e.currentTarget.dataset.type
    var index = e.currentTarget.dataset.index
    this.setData({
      currentType: type,
      currentIndex: index
    })
    var a = this.data
    var addItem = {
      name: a.listData[a.currentType].foods[a.currentIndex].name,
      price: a.listData[a.currentType].foods[a.currentIndex].specfoods[0].price,
      number: 1,
      sum: a.listData[a.currentType].foods[a.currentIndex].specfoods[0].price,
      image: a.listData[a.currentType].foods[a.currentIndex].image_url
    }
    console.log("增添的商品信息addItem", addItem)
    var sumMoney = a.sumMoney + a.listData[a.currentType].foods[a.currentIndex].specfoods[0].price
    var cartList = this.data.cartList
    cartList.push(addItem)
    console.log("商品的总价sumMoney", sumMoney)
    console.log("把商品信息添加到页面carList", cartList)
    this.setData({
      cartList: cartList,
      showModelStatus: false,
      sumMoney: sumMoney,
      cupNumber: Number(a.cupNumber + 1)
    })
  },
  showCartList: function() { //显示已购买的商品
    if (this.data.cartList.length != 0) {
      this.setData({
        showCart: !this.data.showCart
      })
    }
  },
  decNumebr: function(e) { //减少商品数量
    var index = e.currentTarget.dataset.index
    var cartList = this.data.cartList
    var sum = this.data.sumMoney - cartList[index].price
    cartList[index].sum -= cartList[index].price
    cartList[index].number == 1 ? cartList.splice(index, 1) : cartList[index].number--
      this.setData({
        cartList: cartList,
        sumMoney: sum,
        showCart: cartList.length == 0 ? false : true,
        cupNumber: this.data.cupNumber - 1
      })
  },
  addNumber: function(e) { //增加商品数量
    var index = e.currentTarget.dataset.index
    var cartList = this.data.cartList
    cartList[index].number++
      var sum = this.data.sumMoney + cartList[index].price
    cartList[index].sum += cartList[index].price
    this.setData({
      cartList: cartList,
      sumMoney: sum,
      cupNumber: this.data.cupNumber + 1
    })
  },
  clearCartList: function() { //清空购物车
    this.setData({
      cartList: [],
      sumMoney: 0,
      cupNumber: 0,
      showCart: false
    })
  },
  goBalance: function(e) { //跳转到订单页面
    if (this.data.sumMoney == 0) {
      return
    }
    var method = 'post'
    var order_id = this.data.order_id
    console.log("order_id",order_id)
    fetch('food/order', {
        id: 1,
        num: 1
      }, method)
      .then(res => {
        if (res.data.error !== 0) {
          wx.showModal({
            title: '下单失败',
            content: '操作失败请重试'
          })
          return
        }
        wx.navigateTo({
          url: '/pages/order/balance/balance?order_id=' + res.data.order_id,
        })
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})